#include "modelPointTable.h"
#include "db.h"
#include "logger.h"
#include "version.h"
#include <string.h>
#include <stdlib.h>


#define MODEL_POINT_TABLE_NAME "t_model_point"
#define SQL_MODEL_POINT_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " MODEL_POINT_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, modelID INT, name TEXT NOT NULL, dataType INT, classify INT)"
#define SQL_MODEL_POINT_TABLE_INSERT(...) "INSERT INTO " MODEL_POINT_TABLE_NAME "(id, modelID, name, dataType, classify) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_POINT_TABLE_REPLACE(...) "REPLACE INTO " MODEL_POINT_TABLE_NAME "(id, modelID, name, dataType, classify) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_POINT_TABLE_DELETE(id) "DELETE FROM " MODEL_POINT_TABLE_NAME " WHERE id = " #id
#define SQL_MODEL_POINT_TABLE_SELECT(id) "SELECT * FROM " MODEL_POINT_TABLE_NAME " WHERE id = " #id


/*===============================================================*/
/* 模型测点 */
int createModelPointTable(){
    return db_create_table(SQL_MODEL_POINT_TABLE_CREATE);
}

int insertModelPoint(T_MODEL_POINT* modelPoint){
    if(NULL == modelPoint){
        log_file_e(APPNAME, "insert model point is NULL");
        return -1;
    }
    if((NULL == modelPoint->name) || (1 > strlen(modelPoint->name))){
        log_file_e(APPNAME, "insert model point's name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_MODEL_POINT_TABLE_INSERT(modelPoint->id, modelPoint->modelID, modelPoint->name, modelPoint->dataType, modelPoint->classify));
}

int replaceModelPoint(T_MODEL_POINT* modelPoint){
    if(NULL == modelPoint){
        log_file_e(APPNAME, "replace model point is NULL");
        return -1;
    }
    if((NULL == modelPoint->name) || (1 > strlen(modelPoint->name))){
        log_file_e(APPNAME, "replace model point's name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_MODEL_POINT_TABLE_REPLACE(modelPoint->id, modelPoint->modelID, modelPoint->name, modelPoint->dataType, modelPoint->classify));
}

int deleteModelPoint(T_MODEL_POINT* modelPoint){
    if(NULL == modelPoint){
        log_file_e(APPNAME, "delete model point is NULL");
        return -1;
    }
    return db_delete(SQL_MODEL_POINT_TABLE_DELETE(model->id));
}

static int modelPointCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "modelCallback data is NULL");
        return -1;
    }
    T_MODEL_POINT_LIST* modelPointList = (T_MODEL_POINT_LIST*)data;
    if((0 == modelPointList->count) || (NULL == modelPointList->head)){
        modelPointList->head = modelPointList->tail = (T_MODEL_POINT*)malloc(sizeof(T_MODEL_POINT));
        modelPointList->head->next = NULL;
        modelPointList->head->prev = NULL;
    } else {
        modelPointList->tail->next = (T_MODEL_POINT*)malloc(sizeof(T_MODEL_POINT));
        modelPointList->tail->next->prev = modelPointList->tail;
        modelPointList->tail = modelPointList->tail->next;
    }
    modelPointList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            modelPointList->tail->id = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "modelID")){
            modelPointList->tail->modelID = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "dataType")){
            modelPointList->tail->dataType = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "classify")){
            modelPointList->tail->classify = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                modelPointList->tail->name[0] = '\0';
            } else {
                strcpy(modelPointList->tail->name, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return modelPointList->count;
}

int selectModelPoint(const char* sql, T_MODEL_POINT_LIST* modelPointList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == modelPointList)){
        log_file_e(APPNAME, "select sql is NULL");
        return -1;
    }
    return db_select(sql, modelPointCallback, modelPointList);

}
//
